Imports Janus.Windows.GridEX
Public Class frmSETTLE
#Region "Variable"
    Public SubTotal As Double = 0
    Public DiscountAmt As Double = 0
    Public TaxAmt As Double = 0

    Public Total As Double = 0 '=SubTotal-Discount+Tax
    Public SVCAmt As Double = 0
    Public TipAmt As Double = 0
    Dim BillDeposit As Double = 0

    Public AmountDue As Double = 0 'Total+SVCAmt+Tip-BillDeposit
    Dim ReceivedAmt As Double = 0
    Dim RemainAmt As Double = 0

    Public Subscription As Boolean = False
    Public BillCode As Int64 = 0
    Public CustomerCode As String = ""


    Dim index As Integer = 0'1: Cash Amount, 2:Serial Number, 3: Qty, 4: Voucher Amount
    Dim ExchangeRate As Double = 1
    Dim CreditCardCode As String = ""
    Dim ForeignCurrency As String = ""


    'Save actual amount
    Dim CashAmt As Double = 0
    Dim VoucherAmt As Double = 0
    Dim CreditCardAmt As Double = 0
    Dim ForeignAmt As Double = 0
    Dim DebitAmt As Double = 0
    Dim MileageAmt As Double = 0

#End Region
#Region "Control Event"
    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
    Private Sub frmSETTLE_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Try
            GC.Collect()
            Me.Dispose()
        Catch ex As Exception

        End Try
    End Sub
    Private Sub frmSETTLE_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        PublicFunction.NewFormLoad(Me)

        If PublicVariable.ByCreditCard = "0" Then
            tpCreditCard.TabVisible = False
        Else
            GridCreditCard.DataSource = BLL.ObjectGetAll("CREDITCARD_LOADALL")
        End If

        If PublicVariable.ByDebit = "0" Then
            tpDebit.TabVisible = False
        End If
        If PublicVariable.ByMileage = "0" Then
            tpMileage.TabVisible = False
        End If
        If PublicVariable.ByForeign = "0" Then
            tpForeign.TabVisible = False
        End If

        If CustomerCode = "" Then
            tpMileage.Enabled = False
            tpDebit.Enabled = False
        Else
            Dim arName As New ArrayList
            arName.Add("@Condition")
            arName.Add("@Parameter")
            Dim arVal As New ArrayList
            arVal.Add(1)
            arVal.Add(CustomerCode)
            Dim dt As DataTable = BLL.ObjectGetBy("CUSTOMER_SEARCH", arName, arVal)
            If dt.Rows.Count > 0 Then
                vlCustomerName.Text = dt.Rows(0).Item("CUS_NAME").ToString
                vlTotalMileage.Text = String.Format(PublicVariable.NumberFormatString, dt.Rows(0).Item("TOTAL_MILEAGE"))
                vlUsedMileage.Text = String.Format(PublicVariable.NumberFormatString, dt.Rows(0).Item("USED_MILEAGE"))
                Dim RemainMileage As Double = dt.Rows(0).Item("REMAIN_MILEAGE")
                If RemainMileage > 0 Then
                    vlRemainMileage.Text = String.Format(PublicVariable.NumberFormatString, RemainMileage)
                    txtMileage.Enabled = True
                Else
                    vlRemainMileage.Text = 0
                    txtMileage.Enabled = False
                End If


                vlCustomerName1.Text = dt.Rows(0).Item("CUS_NAME").ToString
                vlMaxDebit.Text = String.Format(PublicVariable.NumberFormatString, dt.Rows(0).Item("MAX_DEBIT"))
                vlCurrentDebit.Text = String.Format(PublicVariable.NumberFormatString, dt.Rows(0).Item("CURRENT_DEBIT"))
                Dim RemainDebit As Double = dt.Rows(0).Item("REMAIN_DEBIT")
                If RemainDebit > 0 Then
                    txtDebit.Enabled = True
                Else
                    txtDebit.Enabled = False
                End If
            End If
        End If


        Me.GridSerial.RootTable.Columns("QUANTITY").FormatString = PublicVariable.NumberFormatString
        Me.GridSerial.RootTable.Columns("AMOUNT").FormatString = PublicVariable.NumberFormatString

        Dim dt1 As DataTable = oOrder.LoadBillDetail(BillCode)
        GridOrder.DataSource = dt1
        If dt1.Rows.Count > 0 Then
            'BILL_TOTAL: TOTAL AFTER DISCOUNT AND WITH VAT
            'BILL_AMT: TOTAL BEFORE DISCOUNT AND WITHOUT VAT
            Me.Total = dt1.Rows(0).Item("BILL_TOTAL")
            Me.SVCAmt = dt1.Rows(0).Item("BILL_SVC")
            Me.SubTotal = dt1.Rows(0).Item("BILL_AMT")
            Me.DiscountAmt = dt1.Rows(0).Item("BILL_DCAMT")
            Me.TaxAmt = dt1.Rows(0).Item("BILL_TAX_AMT")
            Me.CustomerCode = dt1.Rows(0).Item("BILL_TOTAL")
            Me.TipAmt = dt1.Rows(0).Item("BILL_TIP")
            Me.BillDeposit = dt1.Rows(0).Item("BILL_DEPOSIT")
            Me.AmountDue = Total + SVCAmt + TipAmt - BillDeposit 'SubTotal - DiscountAmt + SVCAmt + TaxAmt + TipAmt
            Me.RemainAmt = Me.AmountDue
            'Load Total
            vlTotal.Text = String.Format(PublicVariable.NumberFormatString, Total)
            vlAmountDue.Text = String.Format(PublicVariable.NumberFormatString, AmountDue)
            vlAmountDue1.Text = String.Format(PublicVariable.NumberFormatString, AmountDue)
            vlTax.Text = String.Format(PublicVariable.NumberFormatString, TaxAmt)
            vlSubtotal.Text = String.Format(PublicVariable.NumberFormatString, SubTotal)
            vlDiscount.Text = String.Format(PublicVariable.NumberFormatString, DiscountAmt)
            vlTip.Text = String.Format(PublicVariable.NumberFormatString, TipAmt)
            vlServiceCharge.Text = String.Format(PublicVariable.NumberFormatString, SVCAmt)
            vlDeposit.Text = String.Format(PublicVariable.NumberFormatString, BillDeposit)
        End If

        If PublicVariable.ByCash = "0" Then
            tpCash.TabVisible = False
        Else
            GridCurrency.DataSource = BLL.ObjectGetAll("CURRENCY_LOADALL")

            Dim dt As DataTable
            Dim arName As New ArrayList
            arName.Add("@Total")
            arName.Add("@Currency")
            Dim arVal As New ArrayList
            arVal.Add(AmountDue)
            arVal.Add(PublicVariable.LocalCurrency)
            dt = BLL.ObjectGetBy("CURRENCY_RECOMMENDMONEY", arName, arVal)
            GridCash.DataSource = dt
        End If

    End Sub
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If BillCode > 0 Then
            If PublicVariable.IsLicense = False Then
                SystemFunction.ShowMessageForm("PLEASE APPLY LICENSE TO USE THIS FUNCTIONS", "")
                Return
            End If
            If (Total - ReceivedAmt) > 0 And Not Subscription Then
                'if not subscription and received < total
                CashAmt = CashAmt + (Total - ReceivedAmt)
            End If
            Dim arName As New ArrayList
            Dim arVal As New ArrayList
            Dim dt As DataTable

            oSettle.PaymentAdd(BillCode, 1, CashAmt, "", PublicVariable.LocalCurrency, 1, CustomerCode)
            oSettle.PaymentAdd(BillCode, 2, CreditCardAmt, CreditCardCode, PublicVariable.LocalCurrency, 1, CustomerCode)
            oSettle.PaymentAdd(BillCode, 3, VoucherAmt, "", PublicVariable.LocalCurrency, 1, CustomerCode)

            For i As Integer = 0 To GridSerial.RowCount - 1
                GridSerial.Row = i
                arName.Clear()
                arName.Add("@BILLCODE")
                arName.Add("@SERIAL")
                arName.Add("@Quantity")
                arName.Add("@Amount")
                arVal.Clear()
                arVal.Add(BillCode)
                arVal.Add(GridSerial.GetValue("SERIAL").ToString)
                arVal.Add(GridSerial.GetValue("QUANTITY"))
                arVal.Add(GridSerial.GetValue("AMOUNT"))
                dt = BLL.ObjectGetBy("Payment_Voucher_Add", arName, arVal)
                'If dt.Rows.Count > 0 Then
                '    BLL.ReturnXML("PAYMENT_VOUCHER", dt)
                'End If
            Next
            oSettle.PaymentAdd(BillCode, 4, MileageAmt, "", PublicVariable.LocalCurrency, 1, CustomerCode)
            oSettle.PaymentAdd(BillCode, 5, ForeignAmt, "", ForeignCurrency, ExchangeRate, CustomerCode)
            oSettle.PaymentAdd(BillCode, 6, DebitAmt, "", PublicVariable.LocalCurrency, 1, CustomerCode)





            oSettle.SettleBill(vlSubtotal.Text, vlTax.Text, 0, vlDiscount.Text, 0, vlTotal.Text, "", BillCode)
            If PublicVariable.ReceiptPrintAuto = "1" Then
                Dim oPrint As New oPrint
                oPrint.PrintSlip(BillCode, False, PublicVariable.ReceiptPrintFilePath, PublicVariable.ReceiptPrintName, PublicVariable.ReceiptPrintSlipCount)
            End If
        Else
            MessageBox.Show("BILLCODE=0")
        End If
    End Sub
    Private Sub txtCashAmount_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtVoucher.TextChanged, txtQuantity.TextChanged, txtMileage.TextChanged, txtForeignAmt.TextChanged, txtForeign.TextChanged, txtDebit.TextChanged, txtCreditCard.TextChanged, txtCashAmount.TextChanged, txtCardType.TextChanged
        '0. Neu ko phai so, hoac ko con gia tri con lai thi thoat
        '1. Hien thi gia tri da chon
        '2. Chi lay gia tri vua du (luu xuong db)
        '3. Cap nhat gia tri con lai
        Select Case CType(sender, Control).Name
            Case "txtCashAmount"
                '0.
                If Not IsNumeric(txtCashAmount.Text) Then Return
                If RemainAmt <= 0 Then Return
                '1.
                vlCash.Text = String.Format(PublicVariable.NumberFormatString, CDbl(txtCashAmount.Text))
                '2.
                CashAmt = IIf(CDbl(vlCash.Text) > AmountDue, AmountDue, CDbl(vlCash.Text))
                '3.
                ReceivedAmt = CashAmt + VoucherAmt + CreditCardAmt + DebitAmt + MileageAmt + ForeignAmt
                RemainAmt = AmountDue - ReceivedAmt

            Case "txtCreditCard"
                If Not IsNumeric(txtCreditCard.Text) Then Return
                If RemainAmt <= 0 Then Return

                '1.
                vlCreditCard.Text = String.Format(PublicVariable.NumberFormatString, CDbl(txtCreditCard.Text))
                '2.
                CreditCardAmt = IIf(CDbl(vlCreditCard.Text) > AmountDue, AmountDue, CDbl(vlCreditCard.Text))
                '3.
                ReceivedAmt = CashAmt + VoucherAmt + CreditCardAmt + DebitAmt + MileageAmt + ForeignAmt
                RemainAmt = AmountDue - ReceivedAmt

            Case "txtDebit"
                If Not IsNumeric(txtDebit.Text) Then Return
                If RemainAmt <= 0 Then Return
                '1.
                vlDebit.Text = String.Format(PublicVariable.NumberFormatString, CDbl(txtDebit.Text))
                '2.
                DebitAmt = IIf(CDbl(vlDebit.Text) > AmountDue, AmountDue, CDbl(vlDebit.Text))
                '3.
                ReceivedAmt = CashAmt + VoucherAmt + CreditCardAmt + DebitAmt + MileageAmt + ForeignAmt
                RemainAmt = AmountDue - ReceivedAmt

            Case "txtForeignAmt"
                If Not IsNumeric(txtForeignAmt.Text) Then Return
                If RemainAmt <= 0 Then Return

                '1.
                vlForeignAmt.Text = String.Format(PublicVariable.NumberFormatString, CDbl(txtForeignAmt.Text))
                '2.
                ForeignAmt = IIf(CDbl(vlForeignAmt.Text) > AmountDue, AmountDue, CDbl(vlForeignAmt.Text))
                '3.
                ReceivedAmt = CashAmt + VoucherAmt + CreditCardAmt + DebitAmt + MileageAmt + ForeignAmt
                RemainAmt = AmountDue - ReceivedAmt
            Case "txtForeign"
                If Not IsNumeric(txtForeign.Text) Then Return
                If RemainAmt <= 0 Then Return
                vlForeign.Text = String.Format(PublicVariable.NumberFormatString, CDbl(txtForeign.Text))
                txtForeignAmt.Text = String.Format(PublicVariable.NumberFormatString, ExchangeRate * CDbl(txtForeign.Text))
            Case "txtMileage"
                If Not IsNumeric(txtMileage.Text) Then Return
                If RemainAmt <= 0 Then Return
                '1.
                vlMileage.Text = String.Format(PublicVariable.NumberFormatString, CDbl(txtMileage.Text))
                '2.
                MileageAmt = IIf(CDbl(vlMileage.Text) > AmountDue, AmountDue, CDbl(vlMileage.Text))
                '3.
                ReceivedAmt = CashAmt + VoucherAmt + CreditCardAmt + DebitAmt + MileageAmt + ForeignAmt
                RemainAmt = AmountDue - ReceivedAmt

        End Select
        Dim TotalReceived As Double = 0
        TotalReceived = CDbl(vlCash.Text) + _
                    CDbl(vlCreditCard.Text) + CDbl(vlMileage.Text) + _
                    CDbl(vlVoucher.Text) + CDbl(vlDebit.Text) + CDbl(vlForeignAmt.Text)
        vlReceive.Text = String.Format(PublicVariable.NumberFormatString, TotalReceived)
        If TotalReceived > AmountDue Then
            vlChange.Text = String.Format(PublicVariable.NumberFormatString, TotalReceived - AmountDue)
        Else
            vlChange.Text = "0"
        End If
    End Sub
    Private Sub txtCashAmount_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVoucher.Enter, txtSerialNumber.Enter, txtQuantity.Enter, txtMileage.Enter, txtDebit.Enter, txtCreditCard.Enter, txtCashAmount.Enter, txtCardNumber.Enter, txtCardType.Enter
        Select Case CType(sender, Control).Name
            Case "txtCashAmount"
                index = 1
            Case "txtSerialNumber"
                index = 2
            Case "txtQuantity"
                index = 3
            Case "txtVoucher"
                index = 4
            Case "txtCreditCard"
                index = 5
            Case "txtCardNumber"
                index = 6
            Case "txtDebit"
                index = 7
            Case "txtForeignAmt"
                index = 8
            Case "txtForeign"
                index = 9
            Case "txtMileage"
                index = 10
        End Select
    End Sub
    Private Sub NumberKeyPad1_UserKeyPressed(ByVal sender As System.Object, ByVal e As VINAPOS.KeyboardClassLibrarySmall.KeyboardEventArgs) Handles NumberKeyPad1.UserKeyPressed
        Select Case index
            Case 1
                txtCashAmount.Focus()
            Case 2
                txtSerialNumber.Focus()
            Case 3
                txtQuantity.Focus()
            Case 4
                txtVoucher.Focus()
            Case 5
                txtCreditCard.Focus()
            Case 6
                txtCardNumber.Focus()
            Case 7
                txtDebit.Focus()
            Case 8
                txtForeignAmt.Focus()
            Case 9
                txtForeign.Focus()
            Case 10
                txtMileage.Focus()
        End Select
        SendKeys.Send(e.KeyboardKeyPressed)
    End Sub
    Private Sub GridCurrency_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridCurrency.Click
        Try

            ForeignCurrency = GridCurrency.GetValue("CURR_CODE").ToString
            Dim arName As New ArrayList
            Dim arVal As New ArrayList
            arName.Clear()
            arVal.Clear()
            arName.Add("@CURR_CODE")
            arName.Add("@DATE")
            arName.Add("@LOCAL_CURR")
            arVal.Add(ForeignCurrency)
            arVal.Add(Now.Date)
            arVal.Add(PublicVariable.LocalCurrency)
            ExchangeRate = BLL.Object_Update_Result("EXCHANGERATE_GET", arName, arVal)

            txtExchangeRate.Text = String.Format(PublicVariable.NumberFormatString, ExchangeRate)
            txtForeignAmt.Text = String.Format(PublicVariable.NumberFormatString, ExchangeRate * CDbl(txtForeign.Text))
            lblForeignCurr.Text = ForeignCurrency
        Catch ex As Exception

        End Try

    End Sub
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If RemainAmt <= 0 Then Return

        If PublicVariable.CheckSerial = "1" Then
            Dim dt As DataTable
            Dim arName As New ArrayList
            arName.Add("@SERIAL")
            Dim arVal As New ArrayList
            arVal.Add(txtSerialNumber.Text)
            dt = BLL.ObjectGetBy("VOUCHER_CHECKSERIAL", arName, arVal)
            If dt.Rows.Count > 0 Then
                If dt.Rows(0).Item("STATUS") <> 1 Then
                    SystemFunction.ShowMessageForm("CARD IS USED ALREADY!", "")
                    Return
                End If
                Dim type As Integer
                type = dt.Rows(0).Item("TYPE")
                If type = 1 Then 'GIFT AMOUNT
                    txtVoucher.Text = dt.Rows(0).Item("REMAIN")
                ElseIf type = 3 Then ' GIFT PERCENT
                    Dim Percent As Double = dt.Rows(0).Item("REMAIN")
                    txtVoucher.Text = SubTotal * Percent / 100
                End If
            Else
                SystemFunction.ShowMessageForm("SERIAL IS NOT FOUND!", "")
                Return
            End If
        End If
        GridSerial.BoundMode = BoundMode.Unbound
        Dim Col0 As GridEXColumn = Me.GridSerial.RootTable.Columns("SERIAL")
        If GridSerial.FindAll(Col0, ConditionOperator.Equal, txtSerialNumber.Text) > 0 Then
            SystemFunction.ShowMessageForm("SERIAL IS ALREADY ADDED!", "")
            Return
        Else
            Dim newRow As GridEXRow = GridSerial.AddItem()
            newRow.BeginEdit()
            newRow.Cells("SERIAL").Value = txtSerialNumber.Text
            If IsNumeric(txtQuantity.Text) Then
                If CInt(txtQuantity.Text) > 0 Then
                    newRow.Cells("QUANTITY").Value = txtQuantity.Text
                Else
                    newRow.Cells("QUANTITY").Value = 0
                End If
            Else
                newRow.Cells("QUANTITY").Value = 0
            End If
            If IsNumeric(txtVoucher.Text) Then
                If CInt(txtVoucher.Text) > 0 Then
                    newRow.Cells("AMOUNT").Value = txtVoucher.Text
                Else
                    newRow.Cells("AMOUNT").Value = 0
                End If
            Else
                newRow.Cells("AMOUNT").Value = 0
            End If

            newRow.EndEdit()
        End If

        GridSerial.UpdateData()
        txtVoucher.Text = "0"
        txtSerialNumber.Text = ""
        txtQuantity.Text = "1"

        '1.
        Dim GridTotal As Double = 0
        For i As Integer = 0 To GridSerial.RowCount - 1
            GridSerial.Row = i
            GridTotal = GridTotal + GridSerial.GetValue("AMOUNT")
        Next
        vlVoucher.Text = String.Format(PublicVariable.NumberFormatString, GridTotal)
        '2.
        VoucherAmt = IIf(CDbl(vlVoucher.Text) > AmountDue, AmountDue, CDbl(vlVoucher.Text))
        '3.
        ReceivedAmt = CashAmt + VoucherAmt + CreditCardAmt + DebitAmt + MileageAmt + ForeignAmt
        RemainAmt = AmountDue - ReceivedAmt

        Dim TotalReceived As Double = 0
        TotalReceived = CDbl(vlCash.Text) + _
                    CDbl(vlCreditCard.Text) + CDbl(vlMileage.Text) + _
                    CDbl(vlVoucher.Text) + CDbl(vlDebit.Text) + CDbl(vlForeignAmt.Text)
        vlReceive.Text = String.Format(PublicVariable.NumberFormatString, TotalReceived)
        If TotalReceived > AmountDue Then
            vlChange.Text = String.Format(PublicVariable.NumberFormatString, TotalReceived - AmountDue)
        Else
            vlChange.Text = "0"
        End If
    End Sub
    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        GridSerial.CurrentRow.Delete()
        'UpdateTotalBill()
    End Sub
    Private Sub GridCreditCard_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridCreditCard.Click
        txtCardType.Text = GridCreditCard.GetValue("CreditCard").ToString
        CreditCardCode = GridCreditCard.GetValue("Code").ToString
    End Sub
    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim oPrint As New oPrint
        oPrint.PrintSlip(BillCode, PublicVariable.ShowPrintPreview, PublicVariable.ReceiptPrintFilePath, PublicVariable.ReceiptPrintName, PublicVariable.ReceiptPrintSlipCount)
    End Sub
    Private Sub GridCash_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridCash.Click
        If Not GridCash.CurrentRow Is Nothing Then
            txtCashAmount.Text = String.Format(PublicVariable.NumberFormatString, GridCash.GetValue("Amt"))
        End If
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If CDbl(vlChange.Text) > 0 Then
            If vlChange.ForeColor = Color.Black Then
                vlChange.ForeColor = Color.Red
            Else
                vlChange.ForeColor = Color.Black
            End If
        End If
    End Sub
    Private Sub txtVoucher_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVoucher.ButtonClick
        PublicVariable.frmCalculator.Top = txtVoucher.Top + txtVoucher.Height
        PublicVariable.frmCalculator.Left = txtVoucher.Left + txtVoucher.Width
        If PublicVariable.frmCalculator.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtVoucher.Text = String.Format(PublicVariable.NumberFormatString, PublicVariable.frmCalculator.txtResult.Text)
        End If
    End Sub
#End Region


    
End Class